# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None

class TreeNode:
    def __init__(self, x):
        self.val = x
        self.left = None
        self.right = None

# 这个解法基于中序遍历，因为中序遍历后，结果是一个有序列表，所以结果不是有序列表，就不对
# 这个做法在 solution2 上做了改进，不再申请一个列表来存储所有的值，只申请两个变量做比较就好了
# 在 leetcode 上用 python3，这个做法反而更慢……

class X:
    def __init__(self):
        self.val = None

    def append(self, x):
        if self.val == None:
            self.val = x
            return
        if self.val >= x:
            raise ValueError('不是 bst')
        else:
            self.val = x

class Solution:
    def isValidBST(self, root: TreeNode) -> bool:
        list = X()
        try:
            self.__inorderItr(root, list)
            return True
        except ValueError as ex:
            return False

    def __inorderItr(self, node: TreeNode, list):
        if node == None:
            return
        self.__inorderItr(node.left, list)
        list.append(node.val)
        self.__inorderItr(node.right, list)
